﻿using Hetang.Models.Config;
using Hetang.Models.DataContext;
using Hetang.Models.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hetang.Models.Repository
{
    public class SessionRepository : ISessionRepository
    {
        void ISessionRepository.SetLoginSession(tbl_Account account, bool isLogin)
        {
            HttpContext.Current.Session["uid"] = account.Id;
            HttpContext.Current.Session["email"] = account.Email;
            HttpContext.Current.Session["username"] = account.UserName;
            HttpContext.Current.Session["usertype"] = account.User_Type;

            if (isLogin)
            {
                IDatabaseRepository _dr = new DatabaseRepository();
                if (account.User_Type.Equals(UserType.person.ToString()))
                {
                    HttpContext.Current.Session["rid"] = _dr.GetResumeId();
                }
                else if (account.User_Type.Equals(UserType.company.ToString()))
                {
                    HttpContext.Current.Session["cid"] = _dr.GetCompanyId();
                }
            }
        }

        void ISessionRepository.ClearSession()
        {
            HttpContext.Current.Session.RemoveAll();
        }

        UserType ISessionRepository.GetUserType()
        {
            if (HttpContext.Current.Session["usertype"] == null)
            {
                return (Hetang.Models.Config.UserType)Enum.Parse(typeof(Hetang.Models.Config.UserType), "unknow", true);
            }
            else
            {
                return (Hetang.Models.Config.UserType)Enum.Parse(typeof(Hetang.Models.Config.UserType), Convert.ToString(HttpContext.Current.Session["usertype"]), true);
            }
        }

        int ISessionRepository.GetUserId()
        {
            return Convert.ToInt32(HttpContext.Current.Session["uid"]);
        }

        string ISessionRepository.GetEmail()
        {
            return Convert.ToString(HttpContext.Current.Session["email"]);
        }

        string ISessionRepository.GetUserName()
        {
            return Convert.ToString(HttpContext.Current.Session["username"]);
        }

        int ISessionRepository.GetResumeId()
        {
            return Convert.ToInt32(HttpContext.Current.Session["rid"]);
        }

        int ISessionRepository.GetCompanyId()
        {
            return Convert.ToInt32(HttpContext.Current.Session["cid"]);
        }

        void ISessionRepository.SetCompanyId(int id)
        {
            HttpContext.Current.Session["cid"] = id;
        }

        void ISessionRepository.SetResumeId(int id)
        {
            HttpContext.Current.Session["rid"] = id;
        }
    }
}